########## THIS FILE GENERATES FIGURES FROM STATA OUTPUT ###########

##### Importing libraries 

library(rdrobust)
library(foreign)
library(plyr)
library(ggplot2)
library(rdd)



#### Generate Figure 1 (Human Trafficking Attitudes) ####

###### Importing Republican Data Study 1
paneld1a <- read.table("fig1a.txt",header=T, stringsAsFactors=FALSE)
paneld1a$pe <- paneld1a$c1
paneld1a$se <- paneld1a$c2
paneld1a$lb <- paneld1a$pe- 1.96*paneld1a$se
paneld1a$ub <- paneld1a$pe+ 1.96*paneld1a$se

#Create subgroup factors
paneld1a$Population <- 3

paneld1a$group <- factor(paneld1a$c4,levels=c(2,1),
                        labels=c("Concern","Scope \nof Problem")[2:1])

###### Importing Democrat Data Study 1
paneld2a <- read.table("fig2a.txt",header=T, stringsAsFactors=FALSE)
paneld2a$pe <- paneld2a$c1
paneld2a$se <- paneld2a$c2
paneld2a$lb <- paneld2a$pe- 1.96*paneld2a$se
paneld2a$ub <- paneld2a$pe+ 1.96*paneld2a$se

#Create subgroup factors
paneld2a$Population <- 3

paneld2a$group <- factor(paneld2a$c4,levels=c(2,1),
                         labels=c("Concern","Scope \nof Problem")[2:1])



###### Importing Republican Data Study 2

paneld1 <- read.table("fig1.txt",header=T, stringsAsFactors=FALSE)
paneld1$pe <- paneld1$c1
paneld1$se <- paneld1$c2
paneld1$lb <- paneld1$pe- 1.96*paneld1$se
paneld1$ub <- paneld1$pe+ 1.96*paneld1$se

#Create subgroup factors
paneld1$Population <- c(rep(c(3:1),2))

paneld1$group <- factor(paneld1$c4,levels=c(2,1),
                       labels=c("Concern","Scope \nof Problem")[2:1])


###### Importing Democrat Data
paneld2 <- read.table("fig2.txt",header=T, stringsAsFactors=FALSE)
paneld2$pe <- paneld2$c1
paneld2$se <- paneld2$c2
paneld2$lb <- paneld2$pe- 1.96*paneld2$se
paneld2$ub <- paneld2$pe+ 1.96*paneld2$se

#Create subgroup factors
paneld2$Population <- c(rep(c(3:1),2))

paneld2$group <- factor(paneld2$c4,levels=c(2,1),
                       labels=c("Concern","Scope \nof Problem")[2:1])

#Include identifiers and merge data frames together
paneld1$rep <- paneld1a$rep<-  "Republican"
paneld2$rep <- paneld2a$rep <- "Democrat"
paneld1a$study<- paneld2a$study <- "Study 1"
paneld1$study<- paneld2$study <- "Study 2"

paneld <- rbind(paneld1, paneld1a, paneld2, paneld2a)
paneld$Treatment <- as.factor(recode(paneld$Population, "3='Bridging';2='Learning';1='Values'"))
paneld$Treatment <- factor(paneld$Treatment,levels = paneld$Treatment[c(3,2,1)])


#Generate plot
ggplot(data=paneld, mapping=aes(x=group, y=pe, ymin=lb, ymax=ub, width= 0.5, colour = Treatment, shape=Treatment)) + 
  geom_pointrange(size= 0.25, position = position_dodge(width=.5)) + 
  scale_shape_manual(name="Treatment",values=c(4,5,6), c("Values","Learning","Bridging")) +  
  scale_color_manual(name="Treatment",values=c("grey50","grey40","black"), 
                     labels=c("Values","Learning","Bridging")) + 
  scale_fill_manual(values=c("Values","Learning","Bridging")) +
  coord_flip() +
  geom_hline(yintercept=0, linetype="longdash", size=0.5) +
  labs( y= "Treatment Effect", x = "Dependent Variable", color = "Category") +
  guides(colour = guide_legend(reverse=TRUE),shape = guide_legend(reverse=TRUE)) +
  facet_grid(study ~rep, space="free")+
  theme_bw()

ggsave("study_HT.pdf", width = 180, height = 120, units = "mm", dpi=600)

##### Generate Figure 2 (Immigration Attitudes) ######

####### Importing Study 1 Republican data
paneld3a <- read.table("fig3a.txt",header=T, stringsAsFactors=FALSE)
paneld3a$pe <- paneld3a$c1
paneld3a$se <- paneld3a$c2
paneld3a$lb <- paneld3a$pe- 1.96*paneld3a$se
paneld3a$ub <- paneld3a$pe+ 1.96*paneld3a$se

#Create subgroup factors
paneld3a$Population <- 3

paneld3a$group <- factor(paneld3a$c4,levels=c(1),
                        labels=c("Immigration \nRate"))

####### Importing Study 1 Democratic data
paneld4a <- read.table("fig4a.txt",header=T, stringsAsFactors=FALSE)
paneld4a$pe <- paneld4a$c1
paneld4a$se <- paneld4a$c2
paneld4a$lb <- paneld4a$pe- 1.96*paneld4a$se
paneld4a$ub <- paneld4a$pe+ 1.96*paneld4a$se

#Create subgroup factors
paneld4a$Population <- 3

paneld4a$group <- factor(paneld4a$c4,levels=c(1),
                        labels=c("Immigration \nRate"))



####### Importing Republican Study 2 data
paneld3 <- read.table("fig3.txt",header=T, stringsAsFactors=FALSE)
paneld3$pe <- paneld3$c1
paneld3$se <- paneld3$c2
paneld3$lb <- paneld3$pe- 1.96*paneld3$se
paneld3$ub <- paneld3$pe+ 1.96*paneld3$se

#Create subgroup factors
paneld3$Population <- c(rep(c(3:1),2))

paneld3$group <- factor(paneld3$c4,levels=c(6,5,4,3,2,1),
                       labels=c("Immigration \nRate","Mexican \nBorder Wall",
                                "Unaccompanied \nChildren","Path to \nCitizenship",
                                "Illegal \nImmigration \nPolicy","Immigration \nAttitudes \nIndex")[6:1])


####### Importing Democrat data

paneld4 <- read.table("fig4.txt",header=T, stringsAsFactors=FALSE)
paneld4$pe <- paneld4$c1
paneld4$se <- paneld4$c2
paneld4$lb <- paneld4$pe- 1.96*paneld4$se
paneld4$ub <- paneld4$pe+ 1.96*paneld4$se

#Create subgroup factors
paneld4$Population <- c(rep(c(3:1),2))

paneld4$group <- factor(paneld4$c4,levels=c(6,5,4,3,2,1),
                       labels=c("Immigration \nRate","Mexican \nBorder Wall",
                                "Unaccompanied \nChildren","Path to \nCitizenship",
                                "Illegal \nImmigration \nPolicy","Immigration \nAttitudes \nIndex")[6:1])

#Create party identifiers for each data frame and merge
paneld3$rep <- paneld3a$rep <- "Republican"
paneld4$rep <- paneld4a$rep <- "Democrat" 
paneld3a$study <- paneld4a$study <- "Study 1"
paneld3$study <- paneld4$study <- "Study 2"

paneld <- rbind(paneld3, paneld3a, paneld4, paneld4a)
paneld$Treatment <- as.factor(recode(paneld$Population, "3='Bridging';2='Learning';1='Values'"))
paneld$Treatment <- factor(paneld$Treatment,levels = paneld$Treatment[c(3,2,1)])

scaleFUN <- function(x) sprintf("%.2f", x)

#Generate plot
ggplot(data=paneld, mapping=aes(x=group, y=pe, ymin=lb, ymax=ub, width= 0.5, colour = Treatment, shape=Treatment)) + 
  geom_pointrange(size= 0.25, position = position_dodge(width=.5)) + 
  scale_shape_manual(name="Treatment",values=c(4,5,6), labels=c("Values","Learning","Bridging")) +  
  scale_color_manual(name="Treatment",values=c("grey50","grey40","black"), 
                     labels=c("Values","Learning","Bridging")) + 
  scale_fill_manual(values=c("grey50","grey40","black")) +
  scale_y_continuous(labels=scaleFUN) +
  coord_flip() +
  geom_hline(yintercept=0, linetype="longdash", size=0.5) +
  labs( y= "Treatment Effect", x = "Dependent Variable", color = "Category") +
  guides(colour = guide_legend(reverse=TRUE),shape = guide_legend(reverse=TRUE)) +
  facet_grid(study~rep, scales ="free_y", space="free_y")+
  theme_bw()

ggsave("imm.pdf", width = 180, height = 180, units = "mm", dpi=600)


#### Generate Figure 3 (Immigration Attitude Explanatory Variables)

###### Import Republican data

paneld5 <- read.table("fig5.txt",header=T, stringsAsFactors=FALSE)
paneld5$pe <- paneld5$c1
paneld5$se <- paneld5$c2
paneld5$lb <- paneld5$pe- 1.96*paneld5$se
paneld5$ub <- paneld5$pe+ 1.96*paneld5$se

#Create subgroup factors
paneld5$Population <- factor(paneld5$c3,levels=c(3,2,1),
                            labels=c("Bridging","Learning","Values")[1:3] )

paneld5$group <- factor(paneld5$c4,levels=c(5,4,3,2,1),
                       labels=c("Economic \nThreat","Cultural \nThreat","Outgroup","Ingroup","Victim")[5:1])



###### Import Democrat data
paneld6 <- read.table("fig6.txt",header=T, stringsAsFactors=FALSE)
paneld6$pe <- paneld6$c1
paneld6$se <- paneld6$c2
paneld6$lb <- paneld6$pe- 1.96*paneld6$se
paneld6$ub <- paneld6$pe+ 1.96*paneld6$se

#Create subgroup factors
paneld6$Population <- factor(paneld6$c3,levels=c(3,2,1),
                            labels=c("Bridging","Learning","Values")[1:3] )

paneld6$group <- factor(paneld6$c4,levels=c(5,4,3,2,1),
                       labels=c("Economic \nThreat","Cultural \nThreat","Outgroup","Ingroup","Victim")[5:1])

#Create party identifiers for each data frame and merge
paneld5$rep <- "Republican"
paneld6$rep <- "Democrat" 
paneld <- rbind(paneld5, paneld6)

#Generate plot
ggplot(data=paneld, mapping=aes(x=group, y=pe, ymin=lb, ymax=ub, width= 0.5, colour = Population, shape=Population)) + 
  geom_pointrange(size= 0.25, position = position_dodge(width=.5)) + 
  scale_shape_manual(name="Treatment",values=c(4,5,6), labels=paneld$Population) +  
  scale_color_manual(name="Treatment",values=c("grey50","grey40","black"), 
                     labels=paneld$Population) + 
  scale_fill_manual(values=c("grey50","grey40","black")) +
  scale_y_continuous(labels=scaleFUN) +
  coord_flip() +
  geom_hline(yintercept=0, linetype="longdash", size=0.5) +
  labs( y= "Treatment Effect", x = "Dependent Variable", color = "Category") +
  guides(colour = guide_legend(reverse=TRUE),shape = guide_legend(reverse=TRUE)) +
  facet_grid(.~rep, space="free")+
  theme_bw()

ggsave("study2_meds.pdf", width = 180, height = 110, units = "mm", dpi=600)
